package com.neusoft.neuiotms.dcom.converter;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.common.model.Dictionary;
import com.neusoft.neuiotms.common.repository.DictionaryRepository;
import com.neusoft.neuiotms.dcom.dto.PriorityDTO;
import com.neusoft.neuiotms.dcom.model.Priority;
import com.neusoft.neuiotms.dcom.repository.PriorityRepository;

/**
 * @author WYS
 * @date 2020年8月18日 下午2:02:51
 */
@Component
public class PriorityConverter extends AbstractConverter<Priority, PriorityDTO> {

    @Autowired
    private PriorityRepository priorityRepository;
    @Autowired
    private DictionaryRepository dictionaryRepository;

    @Override
    public Priority toModel(final PriorityDTO dto) {
        Priority model;
        if (dto.isNew()) {
            model = new Priority();
            model.setCreateTime(new Date());
        } else {
            model = this.priorityRepository.getOne(dto.getId());
            model.setUpdateTime(new Date());
        }
        model.setCoverageInfluence(dto.getCoverageInfluence());
        model.setPriority(dto.getPriority());
        model.setUrgency(dto.getUrgency());

        return model;
    }

    @Override
    public PriorityDTO toDTO(final Priority model, final boolean forListView) {
        final PriorityDTO dto = new PriorityDTO();
        dto.setId(model.getId());

        dto.setCoverageInfluence(model.getCoverageInfluence());
        if (model.getCoverageInfluence() != null) {
            final Dictionary dic = this.dictionaryRepository.findByCode(model.getCoverageInfluence());
            if (dic != null) {
                dto.setCoverageInfluenceName(dic.getText());
            }
        }
        dto.setPriority(model.getPriority());
        if ("low".equals(model.getPriority())) {
            dto.setPriorityName("低");
        } else if ("middle".equals(model.getPriority())) {
            dto.setPriorityName("中");
        } else if ("high".equals(model.getPriority())) {
            dto.setPriorityName("高");
        }

        if (model.getPriority() != null) {
            final Dictionary dic = this.dictionaryRepository.findByCode(model.getPriority());
            if (dic != null) {
                dto.setPriorityName(dic.getText());
            }
        }
        dto.setUrgency(model.getUrgency());
        if (model.getUrgency() != null) {
            final Dictionary dic = this.dictionaryRepository.findByCode(model.getUrgency());
            if (dic != null) {
                dto.setUrgencyName(dic.getText());
            }
        }
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        return dto;
    }

}
